package idp.dashboard;

import java.io.IOException;

import idp.communication.MessageController;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

public class InputController extends Thread {
	private Controller _input;
	private int roerStand = 90;
	private int laatsteRoerStand = 90;
	boolean motorAan = false;
	boolean motorSwitched = false;
	int motorSnelheid = 0;
	int laatsteMotorSnelheid = 0;
	int gzeil = 90;
	int laatsteGzeil = 90;
	int fzeil = 90;
	int laatsteFzeil = 90;
	boolean autoZeil = true, autoZeilSwitched = false;

	public InputController() {
		super("InputController");
		Controller[] controllers = ControllerEnvironment
				.getDefaultEnvironment().getControllers();

		for (Controller c : controllers) {
			if (c.getType() == Controller.Type.GAMEPAD) {
				_input = c;
			}
			System.out.println("input name: " + c.getName());
		
		}
		if (_input != null) {
			Component[] components = _input.getComponents();
			for (Component c : components) {
				System.out.println("component name: " + c.getName()
						+ "; analog: " + c.isAnalog() + "; relative: "
						+ c.isRelative());
			}
		} else {
			// Couldn't find a gamepad
			System.out.println("Unable to find a gamecontroller");
			System.exit(0);
		}
	}

	public void run() {
		while (true) {
			_input.poll();
			Component[] components = _input.getComponents();
			for (Component c : components) {
				if (c.getName().equals("x")) {
					handleRoer(c.getPollData());
				} else if (c.getName().equals("y")) {
					handleMotor(c.getPollData());
				} else if (c.getName().equals("A")) {
					handleMotorSwitch(c.getPollData());
				} else if (c.getName().equals("Left Thumb")) {
					if (c.getPollData() == 1f && gzeil < 180 && !autoZeil) {
						gzeil += 10;
					}
				} else if (c.getName().equals("Y")) {
					if (c.getPollData() == 1f && gzeil > 0 && !autoZeil) {
						gzeil -= 10;
					}
				} else if (c.getName().equals("Right Thumb")) {
					if (c.getPollData() == 1f && fzeil < 180 && !autoZeil) {
						fzeil += 10;
					}
				} else if (c.getName().equals("Z")) {
					if (c.getPollData() == 1f && fzeil > 0 && !autoZeil) {
						fzeil -= 10;
					}
				} else if (c.getName().equals("X")) {
					handleZeilSwitch(c.getPollData());
				}
			}
			handleGzeil();
			handleFzeil();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void handleZeilSwitch(float pollData) {
		if (pollData == 1f) {
			if (!autoZeilSwitched) {
				autoZeil = !autoZeil;
				MainFrame.getInstance().dashboard.setManualMode(autoZeil);
			}
			autoZeilSwitched = true;
		} else {
			autoZeilSwitched = false;
		}
	}

	private void handleFzeil() {
		if (laatsteFzeil != fzeil && !autoZeil) {
			laatsteFzeil = fzeil;
			if (MessageController.getInstance() != null) {
				MessageController.getInstance().SendMessageToDevice(
						"fzeil " + fzeil);
			}
		}
	}

	private void handleGzeil() {
		if (laatsteGzeil != gzeil && !autoZeil) {
			laatsteGzeil = gzeil;
			if (MessageController.getInstance() != null) {
				MessageController.getInstance().SendMessageToDevice(
						"gzeil " + gzeil);
			}
		}
	}

	private void handleMotorSwitch(float pollData) {		
		if (pollData == 1f) {
			if (!motorSwitched) {
				motorAan = !motorAan;
				MainFrame.getInstance().dashboard.setMotorMode(motorAan);
			}
			motorSwitched = true;
		} else {
			motorSwitched = false;
		}

	}

	private void handleMotor(float pollData) {
		motorSnelheid = (int) ((pollData - 0.003921628) * -100);
		if (motorSnelheid != laatsteMotorSnelheid && motorAan) {
			laatsteMotorSnelheid = motorSnelheid;
			if (motorSnelheid < -100) {
				motorSnelheid = -100;
			}
			if (motorSnelheid > 100) {
				motorSnelheid = 100;
			}
			 if (MessageController.getInstance() != null)
			 MessageController.getInstance().SendMessageToDevice(
			 "motor " + motorSnelheid);
		} else if (laatsteMotorSnelheid != 0 && !motorAan) {
			laatsteMotorSnelheid = 0;
			if (MessageController.getInstance() != null)
				MessageController.getInstance().SendMessageToDevice("motor 0");
		}
	}

	private void handleRoer(float pollData) {
		roerStand = (int) (((pollData - 0.003921628) * 90) + 90);
		if (roerStand < 0) {
			roerStand = 0;
		}
		if (roerStand > 180) {
			roerStand = 180;
		}
		if (laatsteRoerStand != roerStand) {
			laatsteRoerStand = roerStand;
			if (MessageController.getInstance() != null) {
				MessageController.getInstance().SendMessageToDevice(
						"roer " + roerStand);
				MainFrame.getInstance().dashboard.setRoer(roerStand);
			}
		}
	}
}
